/**
 * 报警分析模块的模拟数据
 */
const AlarmAnalysisMockData = (function() {
    // 报警级别
    const ALARM_LEVELS = {
        HIGH: '高',
        MEDIUM: '中',
        LOW: '低'
    };

    // 报警状态
    const ALARM_STATUS = {
        ACTIVE: '未处理',
        HANDLED: '已处理',
        IGNORED: '已忽略'
    };

    // 模拟粮情报警数据
    function getGrainAlarms(params) {
        const { timeRange, alarmType, alarmLevel, page, pageSize } = params;
        
        // 生成模拟数据
        const alarms = generateMockGrainAlarms(timeRange, alarmType, alarmLevel, page, pageSize);
        
        // 返回模拟响应
        return {
            success: true,
            message: '获取粮情报警数据成功',
            data: {
                data: alarms,
                totalCount: 50,
                page: page,
                pageSize: pageSize
            }
        };
    }

    // 生成模拟粮情报警数据
    function generateMockGrainAlarms(timeRange, alarmType, alarmLevel, page, pageSize) {
        const alarms = [];
        const types = ['温度', '湿度'];
        const levels = ['HIGH', 'MEDIUM', 'LOW'];
        const statuses = ['ACTIVE', 'HANDLED', 'IGNORED'];
        
        // 根据筛选条件过滤
        const filteredTypes = alarmType !== 'all' ? 
            [alarmType === 'temperature' ? '温度' : '湿度'] : 
            types;
        
        const filteredLevels = alarmLevel !== 'all' ? 
            [alarmLevel.toUpperCase()] : 
            levels;
        
        // 生成数据
        for (let i = 0; i < pageSize; i++) {
            const type = filteredTypes[Math.floor(Math.random() * filteredTypes.length)];
            const level = filteredLevels[Math.floor(Math.random() * filteredLevels.length)];
            const status = Math.random() > 0.5 ? 'ACTIVE' : statuses[Math.floor(Math.random() * statuses.length)];
            
            const alarm = {
                id: (page - 1) * pageSize + i + 1,
                time: getRandomTime(timeRange),
                location: `仓库${Math.floor(Math.random() * 5) + 1}-测点${Math.floor(Math.random() * 10) + 1}`,
                type: type,
                value: type === '温度' ? (Math.random() * 10 + 30).toFixed(1) : (Math.random() * 20 + 60).toFixed(1),
                unit: type === '温度' ? '℃' : '%',
                threshold: type === '温度' ? '5-35℃' : '45-75%',
                level: level,
                status: status,
                description: `${type}异常，超出正常范围`
            };
            
            alarms.push(alarm);
        }
        
        return alarms;
    }

    // 模拟气体报警数据
    function getGasAlarms(params) {
        const { timeRange, gasType, alarmLevel, page, pageSize } = params;
        
        // 生成模拟数据
        const alarms = generateMockGasAlarms(timeRange, gasType, alarmLevel, page, pageSize);
        
        // 返回模拟响应
        return {
            success: true,
            message: '获取气体报警数据成功',
            data: {
                data: alarms,
                totalCount: 50,
                page: page,
                pageSize: pageSize
            }
        };
    }

    // 生成模拟气体报警数据
    function generateMockGasAlarms(timeRange, gasType, alarmLevel, page, pageSize) {
        const alarms = [];
        const types = ['氧气', '二氧化碳', '磷化氢'];
        const levels = ['HIGH', 'MEDIUM', 'LOW'];
        const statuses = ['ACTIVE', 'HANDLED', 'IGNORED'];
        
        // 根据筛选条件过滤
        const filteredTypes = gasType !== 'all' ? 
            [gasType === 'oxygen' ? '氧气' : (gasType === 'co2' ? '二氧化碳' : '磷化氢')] : 
            types;
        
        const filteredLevels = alarmLevel !== 'all' ? 
            [alarmLevel.toUpperCase()] : 
            levels;
        
        // 生成数据
        for (let i = 0; i < pageSize; i++) {
            const type = filteredTypes[Math.floor(Math.random() * filteredTypes.length)];
            const level = filteredLevels[Math.floor(Math.random() * filteredLevels.length)];
            const status = Math.random() > 0.5 ? 'ACTIVE' : statuses[Math.floor(Math.random() * statuses.length)];
            
            let value, unit, threshold;
            
            if (type === '氧气') {
                value = (Math.random() * 5 + 17).toFixed(1);
                unit = '%';
                threshold = '19.5-23.5%';
            } else if (type === '二氧化碳') {
                value = (Math.random() * 2000 + 1000).toFixed(0);
                unit = 'ppm';
                threshold = '<5000ppm';
            } else { // 磷化氢
                value = (Math.random() * 0.5).toFixed(2);
                unit = 'ppm';
                threshold = '<0.3ppm';
            }
            
            const alarm = {
                id: (page - 1) * pageSize + i + 1,
                time: getRandomTime(timeRange),
                location: `仓库${Math.floor(Math.random() * 5) + 1}-测点${Math.floor(Math.random() * 10) + 1}`,
                type: type,
                value: value,
                unit: unit,
                threshold: threshold,
                level: level,
                status: status,
                description: `${type}浓度异常，超出安全范围`
            };
            
            alarms.push(alarm);
        }
        
        return alarms;
    }

    // 处理粮情报警
    function handleGrainAlarm(params) {
        return {
            success: true,
            message: '处理粮情报警成功',
            data: null
        };
    }

    // 忽略粮情报警
    function ignoreGrainAlarm(params) {
        return {
            success: true,
            message: '忽略粮情报警成功',
            data: null
        };
    }

    // 处理气体报警
    function handleGasAlarm(params) {
        return {
            success: true,
            message: '处理气体报警成功',
            data: null
        };
    }

    // 忽略气体报警
    function ignoreGasAlarm(params) {
        return {
            success: true,
            message: '忽略气体报警成功',
            data: null
        };
    }

    // 获取随机时间
    function getRandomTime(timeRange) {
        const now = new Date();
        let date = new Date(now);
        
        switch (timeRange) {
            case 'today':
                // 今天内的随机时间
                date.setHours(Math.floor(Math.random() * 24));
                date.setMinutes(Math.floor(Math.random() * 60));
                break;
            case 'week':
                // 过去7天内的随机时间
                date.setDate(date.getDate() - Math.floor(Math.random() * 7));
                date.setHours(Math.floor(Math.random() * 24));
                date.setMinutes(Math.floor(Math.random() * 60));
                break;
            case 'month':
                // 过去30天内的随机时间
                date.setDate(date.getDate() - Math.floor(Math.random() * 30));
                date.setHours(Math.floor(Math.random() * 24));
                date.setMinutes(Math.floor(Math.random() * 60));
                break;
            default:
                // 默认为今天
                date.setHours(Math.floor(Math.random() * 24));
                date.setMinutes(Math.floor(Math.random() * 60));
        }
        
        // 格式化时间
        return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
    }

    // 暴露公共方法
    return {
        getGrainAlarms,
        getGasAlarms,
        handleGrainAlarm,
        ignoreGrainAlarm,
        handleGasAlarm,
        ignoreGasAlarm
    };
})();

// 将模块暴露到全局
window.AlarmAnalysisMockData = AlarmAnalysisMockData; 